package section17;

import java.sql.*;
import java.util.Scanner;

public class App17_1 {
    //jdbc新版本的设备驱动和连接字符串
    private static String driver="com.mysql.cj.jdbc.Driver";
    private static String url="jdbc:mysql://localhost:3306/stu?characterEncoding=utf-8&serverTimezone=UTC";

    private static String user;
    private static String passwd;
    public static void main(String[] args)throws ClassNotFoundException {
        System.out.println("-----------------连接数据库-----------------");
        Scanner in_user=new Scanner(System.in);
        System.out.print("输入用户名：");
        user=new Scanner(System.in).next();
        System.out.print("输入密码：");
        passwd=new Scanner(System.in).next();
        System.out.println("user:"+user+"   passwd:"+passwd);
        String sql="select * from student where SSex='女' and SDept='CS';";
        try (   //建立驱动程序与数据库之间的连接
                Connection conn= DriverManager.getConnection(url,user,passwd);
                //利用连接对象conn创建Statement接口对象
                Statement stmt= conn.createStatement();
                ResultSet rs=stmt.executeQuery(sql)
                ){
            Class.forName(driver);//加载驱动程序
            System.out.println("学号       姓名    性别   年龄     系别    政治面貌");
            while (rs.next()){
                String no=rs.getString("SNo");
                String name=rs.getString("SName");
                String sex=rs.getString("SSex");
                Short age=rs.getShort("SAge");
                String dept=rs.getString("SDept");
                String zzmm=rs.getString("szzmm");

                System.out.println(no+" | "+name+" | "+sex+" | "+age+" | "+dept+" | "+zzmm);
            }
        }
        catch (SQLException sqle){
            sqle.printStackTrace();//输出当前异常对象的堆栈使用痕迹
        }




    }
}
